package com.azt.api.pojo;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;

import com.azt.pojo.BaseEntity;
import com.azt.utils.FormatUtil;
import com.azt.utils.PatternUtils;

/**
 * 用户信息表 Created by shangwq on 2016/8/4.
 */
@Table(name = "u_user")
public class User extends BaseEntity {

	@Column(name = "companyId")
	private Integer companyId;

	@Column(name = "mobile")
	private String mobile;

	@Column(name = "email")
	private String email;

	@Column(name = "contactTel")
	private String contactTel;

	@Column(name = "authString")
	private String authString;

	@Column(name = "pwdString")
	private String pwdString;

	@Column(name = "mailActive")
	private Integer mailActive;

	@Column(name = "password")
	private String password;

	@Column(name = "salt")
	private String salt;

	@Column(name = "registertime")
	private Date registertime;

	@Column(name = "removed")
	private Integer removed;

	@Column(name = "logintime")
	private Date logintime;

	@Column(name = "realName")
	private String realName;

	@Column(name = "gender")
	private String gender;

	@Column(name = "logo")
	private String logo;

	@Column(name = "birthday")
	private Date birthday;

	@Column(name = "promotionLink")
	private String promotionLink;

	@Column(name = "promotionUser")
	private String promotionUser;

	@Column(name = "openId")
	private String openId;

	@Column(name = "unionId")
	private String unionId;

	@Column(name = "state")
	private Integer state;

	@Transient
	private Company company;

	@Transient
	private List<Role> roles;

	@Transient
	private String rolenames;

	@Transient
	private String hideMobile = "";

	@Transient
	private String hideEmail = "";

	@Transient
	private int roleId;// 员工角色

	@Transient
	private int originRoleid;

	@Transient
	private String hiddenUserName;//自动判断是手机号还是邮箱，用****隐藏

	public int getOriginRoleid() {
		return originRoleid;
	}

	public void setOriginRoleid(int originRoleid) {
		this.originRoleid = originRoleid;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public String getContactTel() {
		return contactTel;
	}

	public void setContactTel(String contactTel) {
		this.contactTel = contactTel;
	}

	public Integer getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Integer companyId) {
		this.companyId = companyId;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getHideMobile() {
		return FormatUtil.getHideMobile(mobile);
	}

	public void setHideMobile(String hideMobile) {
		this.hideMobile = hideMobile;
	}

	public String getHideEmail() {
		if (StringUtils.isNotBlank(email) && PatternUtils.isEmail(email)) {
			String str = email.substring(0, email.lastIndexOf('@'));
			Integer len = str.length();
			int start = 0, hide = 1, end = 0;
			switch (len) {
			case (2): {
				start = 1;
				break;
			}
			case (3): {
				start = 1;
				end = 1;
				break;
			}
			case (4): {
				start = 2;
				end = 1;
				break;
			}
			default: {
				start = 2;
				end = 2;
				hide = len - start - end;
				break;
			}
			}
			StringBuffer sb = new StringBuffer(str.substring(0, start));
			for (int i = 0; i < hide; i++) {
				sb.append("*");
			}
			sb.append(str.substring(start + hide));
			sb.append(email.substring(email.lastIndexOf('@')));
			hideEmail = sb.toString();
		}
		return hideEmail;
	}

	public void setHideEmail(String hideEmail) {
		this.hideEmail = hideEmail;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAuthString() {
		return authString;
	}

	public void setAuthString(String authString) {
		this.authString = authString;
	}

	public String getPwdString() {
		return pwdString;
	}

	public void setPwdString(String pwdString) {
		this.pwdString = pwdString;
	}

	public Integer getMailActive() {
		return mailActive;
	}

	public void setMailActive(Integer mailActive) {
		this.mailActive = mailActive;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getLogo() {
		return logo;
	}

	public void setLogo(String logo) {
		this.logo = logo;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Date getRegistertime() {
		return registertime;
	}

	public void setRegistertime(Date registertime) {
		this.registertime = registertime;
	}

	public Integer getRemoved() {
		return removed;
	}

	public void setRemoved(Integer removed) {
		this.removed = removed;
	}

	public Date getLogintime() {
		return logintime;
	}

	public void setLogintime(Date logintime) {
		this.logintime = logintime;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getPromotionLink() {
		return promotionLink;
	}

	public void setPromotionLink(String promotionLink) {
		this.promotionLink = promotionLink;
	}

	public String getPromotionUser() {
		return promotionUser;
	}

	public void setPromotionUser(String promotionUser) {
		this.promotionUser = promotionUser;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getUnionId() {
		return unionId;
	}

	public void setUnionId(String unionId) {
		this.unionId = unionId;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public String getRolenames() {
		/*
		 * for (UserRole ur : this.userroles) { rolenames +=
		 * ur.getRole().getName() + ","; } if (rolenames.length() > 0) {
		 * rolenames = rolenames.substring(0, rolenames.length() - 1); }
		 */
		return rolenames;
	}

	public void setRolenames(String rolenames) {
		this.rolenames = rolenames;
	}

	public String getHiddenEmail() {
		return FormatUtil.getHiddenEmail(email);
	}

	public String getHiddenUserName() {
		String hiddenMobile = getHideMobile();
		String hiddenEmail = getHiddenEmail();
		return StringUtils.isNotBlank(hiddenMobile) ? hiddenMobile : hiddenEmail;
	}


}
